Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Generics → Lower Bounded Wildcards

Generics

Lower Bounded Wildcards

In Java, lower-bounded wildcards (represented by `<? super T>`) specify that a generic type parameter can accept a type that is a supertype (or itself) of a particular type `T`. This means the wildcard can represent any class that is either `T` or a parent class of `T` in the inheritance hierarchy. This is useful when you want to work with a collection of objects that share a common ancestor but might have different specific types.

Understanding the Concept with example

Imagine you have a method that needs to process a list of `Number` objects. This list could contain `Integer`, `Double`, `Float`, etc., as these are all subtypes of `Number`. A lower-bounded wildcard allows you to achieve this flexibility:
Java Lower Bounded Wildcards in Generics example import java.util.List; import java.util.ArrayList; public class Main { // Method to add numbers to a list public static void addNumbers(List<? super Integer> list) { for (int i = 1; i <= 5; i++) { list.add(i); } } public static void main(String[] args) { List<Number> numberList = new ArrayList<>(); addNumbers(numberList); System.out.println("Number List: " + numberList); List<Object> objectList = new ArrayList<>(); addNumbers(objectList); System.out.println("Object List: " + objectList); } }

Output

Number List: [1, 2, 3, 4, 5] Object List: [1, 2, 3, 4, 5]
Explanation `List<? super Number>`: This declares a list whose generic type parameter can be `Number` or any supertype of `Number` (like `Object`).

Why `Object` in the loop?

Because we don't know the exact type within the list (it could be `Integer`, `Double`, etc.), we use `Object` to access elements. This is a slight limitation – we can't directly call `Number`-specific methods without casting. Why it works `Integer` is a subtype of `Number`, and `Number` is a subtype of `Object`. The `List<Integer>` is considered a subtype of `List<? super Number>`. The method can accept any list containing objects that are `Number` or a superclass of `Number`. Limitations and Important Considerations Adding elements A crucial restriction with lower-bounded wildcards is that you can only add elements of the lower bound type (`Number` in our example) or its subtypes. You cannot add anything else.
limitation in adding elements public static void addNumberToList(List<? super Number> numbers) { numbers.add(1); // This is OK numbers.add(1.0); // This is OK //numbers.add("hello"); // This will give a compilation error because String is not a subtype of Number }
Retrieving elements You can only retrieve elements as `Object` and need to cast them to the specific type if needed. This involves a runtime risk of a `ClassCastException` if the cast is incorrect.
When to Use Lower-Bounded Wildcards Lower-bounded wildcards are most beneficial when you need to write methods that can accept lists of different types that share a common ancestor, but you want to restrict the types that can be *added* to the list. This ensures type safety while providing flexibility in the types of lists you can pass to the method. They're less common than upper-bounded wildcards (`<? extends T>`), but they have their place in specific scenarios.

Tutorials